package com.cn.web.controller.EM.partRegt;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.TechnologicalStandard.GrindingProcessStandardMain;
import com.cn.bean.vo.EM.partRegt.BarkPar;
import com.cn.bean.vo.EM.partRegt.PartSingleRegisterBean;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db5.EM.partRegt.PartSingleRegisterDao;
import com.cn.service.server.EM.partRegt.PartSingleRegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Slf4j
@Transactional
@RequestMapping("/Part")
public class PartSingleRegisterController {
    private final PartSingleRegisterService partSingleRegisterService;
    private final PartSingleRegisterDao partSingleRegisterDao;

    @Autowired(required = false)
    public PartSingleRegisterController(PartSingleRegisterDao partSingleRegisterDao, PartSingleRegisterService partSingleRegisterService) {
        this.partSingleRegisterService = partSingleRegisterService;
        this.partSingleRegisterDao = partSingleRegisterDao;
    }

    @RequestMapping("/select/EquipmentMechanicalStructureMapping")
    @ResponseBody
    public List<PartSingleRegisterBean> selectEquipmentMechanicalStructureMappingTable(@RequestBody PartSingleRegisterBean partSingleRegisterBean) {
        System.out.println(partSingleRegisterBean.getComponentName());
        return partSingleRegisterService.getEquipmentMappingTable(partSingleRegisterBean);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(@RequestBody JSONObject jsonObject) {
        PartSingleRegisterBean formMainTable = jsonObject.getObject("formMainTable", PartSingleRegisterBean.class);
        formMainTable.setCreationTime(formMainTable.getCreationTime().replace("/", "-"));
        return partSingleRegisterService.insert(formMainTable);
    }

    @RequestMapping("/getList")
    @ResponseBody
    public List<PartSingleRegisterBean> getList(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        Integer limit = jsonObject.getInteger("limit");
        Integer page = jsonObject.getInteger("page");

        return partSingleRegisterService.getList(limit, page, id);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result update(@RequestBody JSONObject jsonObject) {
        PartSingleRegisterBean formMainTable = jsonObject.getObject("formMainTable", PartSingleRegisterBean.class);

        return partSingleRegisterService.update(formMainTable);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody JSONObject jsonObject) {

        JSONArray idList = jsonObject.getJSONArray("idList");
        List<Integer> id = idList.toJavaList(Integer.class);
        return partSingleRegisterService.delete(id);
    }

    @RequestMapping("/auditing")
    @ResponseBody
    public Result auditing(@RequestBody JSONObject jsonObject) {

        JSONArray idList = jsonObject.getJSONArray("idList");
        List<Integer> id = idList.toJavaList(Integer.class);
        Integer state = jsonObject.getInteger("state");
        return partSingleRegisterService.auditing(id, state);
    }

    @RequestMapping("/singleList")
    @ResponseBody
    public String singleList(@RequestBody JSONObject jsonObject) {

        System.out.println("aaaaaaaaaaaaaa:" + jsonObject);
        return partSingleRegisterDao.singleList(jsonObject.toJSONString());
    }
    @RequestMapping("/insertSingle")
    @ResponseBody
    public BarkPar insertSingle(@RequestBody JSONObject jsonObject) {

        System.out.println("eaaaaaaaaa:" + jsonObject);
        return partSingleRegisterDao.insertSingle(jsonObject.toJSONString());
//        return "10";
    }
    @RequestMapping("/stateUp")
    @ResponseBody
    public BarkPar stateUp(@RequestBody JSONObject jsonObject) {
        System.out.println("cccccccc:" + jsonObject);
        return partSingleRegisterDao.stateUp(jsonObject.toJSONString());

    }
}
